﻿
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Imports DevExpress.XtraEditors




Public Class Form11

    Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Load form size and position
        FormPos(My.Settings.Form11Pos)
        Me.Location = My.Settings.Form11Pos
        Me.Size = My.Settings.Form11Size
        Me.RibbonControl1.SelectedPage = RBLayout

        'Load data
        Me.F1001TableAdapter.Fill(Me.CADISDataSet1.F1001)
        Me.V1004TableAdapter.Fill(Me.CadisPivot.V1004)

        'Load layout if exist
        If File.Exists(My.Settings.LayoutPath) Then
            PivotGridControl1.RestoreLayoutFromXml(My.Settings.LayoutPath)
            BarStaticItem1.Caption = "Loaded format: " & My.Settings.LayoutPath
        End If

    End Sub

    Private Sub Form11_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        'Save form size and position
        My.Settings.Form11Pos = Me.Location
        My.Settings.Form11Size = Me.Size

    End Sub

    Private Sub BarButtonItem1_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick

        'Start print preview
        PivotGridControl1.OptionsPrint.VerticalContentSplitting =
            DevExpress.XtraPrinting.VerticalContentSplitting.Smart
        PivotGridControl1.OptionsPrint.PageSettings.Landscape = True
        PivotGridControl1.ShowPrintPreview()

    End Sub

    Private app As Application

    Private Sub BarButtonItem2_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick

        'Load data to excel
        Const filename As String = "c:\JLD004_Form11_Data.xls"

        If File.Exists(filename) Then
            File.Delete(filename)
        End If

        PivotGridControl1.ExportToXls(filename)

        app = New Application
        app.Visible = True

        app.Workbooks.Open(System.IO.Path.GetFullPath(filename))

    End Sub

    Private Sub BarButtonItem3_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick

        'Save pivot configuration
        Try
            Directory.CreateDirectory("c:\JLD004 Interface M3 Cadis")
        Catch ex As Exception
        End Try

        Dim saveFileDialog1 As System.Windows.Forms.SaveFileDialog
        saveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        saveFileDialog1.FileName = "Turn over"
        saveFileDialog1.InitialDirectory = "c:\JLD004 Interface M3 Cadis"
        saveFileDialog1.Filter = "JLD004 Interface M3 Cadis (*.jld)|*.jld"

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            PivotGridControl1.SaveLayoutToXml(saveFileDialog1.FileName)
        End If

    End Sub

    Private Sub BarButtonItem4_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick

        'Load pivot configuration
        Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog
        openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        openFileDialog1.FilterIndex = 1
        openFileDialog1.InitialDirectory = "c:\JLD004 Interface M3 Cadis"
        openFileDialog1.Filter = "JLD004 Interface M3 Cadis (*.jld)|*.jld"

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            PivotGridControl1.RestoreLayoutFromXml(openFileDialog1.FileName)
            My.Settings.LayoutPath = openFileDialog1.FileName
        End If

        BarStaticItem1.Caption = "Loaded format: " & openFileDialog1.FileName

    End Sub

End Class